package com.link.base.base.agreement.controller;

import com.link.base.base.agreement.model.Agreement;
import com.link.base.base.agreement.service.AgreementService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.LovUtil;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author unknwon
 * @version 1.0
 * @date: 2018/11/8 10:33
 */

@Controller
@RequestMapping("/link/agreement")
public class AgreementController extends BasicController<Agreement> {

    @Resource
    private AgreementService agreementService;
    @Resource
    private KeyGenerateService keyGenerateService;

    @Override
    public BasicService<Agreement> getBasicService() throws Exception {
        return agreementService;
    }

    /**
     * 查询前操作
     *
     * @param entity
     * @param request
     * @throws Exception
     */
    @Override
    public void beforQueryAllOrExample(Agreement entity, HttpServletRequest request) throws Exception {
        if (null == entity.getId()) {
            if (StringUtils.isBlank(entity.getAgrCategory())) {
                entity.setAgrCategory("noMatch");
            }
        } else {
            entity.setAgrCategory(entity.getAttr1());
        }
    }

    @RequestMapping(value = "/queryDomesticAgrListPage")
    @ResponseBody
    public Map<String, Object> queryActualListPage(@JsonParam QueryParams qps, @JsonParam Agreement entity, HttpSession session,
                                                   HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessageKey("queryDomesticAgrListPage").withMessage("查询国内合同审批列表！").flush();
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.addFilter(new Filter("agrCategory", "DomesticAgr"));
            qps.preDealSecurity(request);
            Agreement t = (Agreement) BasicModel.transformClass(entity.getClass(), qps);
            List<Agreement> list = agreementService.queryByExamplePage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Agreement>());
            result.put("result", e.getMessage());
        }
        return result;
    }


    @RequestMapping(value = "/queryIntlAgrListPage")
    @ResponseBody
    public Map<String, Object> queryPlanListPage(@JsonParam QueryParams qps, @JsonParam Agreement entity, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessageKey("queryDomesticAgrListPage").withMessage("查询国际合同审批列表！").flush();
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.preDealSecurity(request);
            Agreement t = (Agreement) BasicModel.transformClass(entity.getClass(), qps);
            List<Agreement> list = agreementService.queryByExamplePage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Agreement>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 国内合同审批表新建默认参数
     *
     * @param qps
     * @param request
     * @return
     */
    @RequestMapping(value = "/domesticNewInit")
    @ResponseBody
    public Map<String, Object> domesticNewInit(@JsonParam QueryParams qps, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        Agreement entity = new Agreement();
        try {
            entity.setId(keyGenerateService.keyGenerate());
            entity.setAgrCategory(LovUtil.getLovName("AGR_CATEGORY", "DomesticAgr"));
            result.put("success", true);
            result.put("result", entity);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 国际合同审批表新建默认参数
     *
     * @param qps
     * @param request
     * @return
     */
    @RequestMapping(value = "/intlNewInit")
    @ResponseBody
    public Map<String, Object> intlNewInit(@JsonParam QueryParams qps, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        Agreement entity = new Agreement();
        try {
            entity.setId(keyGenerateService.keyGenerate());
            entity.setAgrCategory(LovUtil.getLovName("AGR_CATEGORY", "InternationalAgr"));
            result.put("success", true);
            result.put("result", entity);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 提交合同审批表
     *
     * @param record
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> submit(@JsonParam Agreement record, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            agreementService.submit(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 更新头行信息-interface用到
     *
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/upsertHeadAndItem", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> upsertHeadAndItem(@RequestBody Agreement model,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            Agreement agreement = agreementService.upsertHeadAndItem(model);
            result.put("success", true);
            result.put("crmId", agreement.getId());
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
            LCLogger.withException(e);
        }
        return result;
    }


    /**
     * 批量更新头行信息-interface用到
     *
     * @param list
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/batchUpsertHeadAndItem", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchUpsertHeadAndItem(@RequestBody List<Agreement> list,
                                                      HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            agreementService.batchUpsertHeadAndItem(list);
            result.put("success", true);
            result.put("list", list);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
            LCLogger.withException(e);
        }
        return result;
    }


    /**
     * 根据crmId查询合同信息-interface用到
     *
     * @param crmId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/get", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> get(@RequestParam Long crmId,
                                   HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        Long agrId = crmId;
        try {
            if (null == agrId) {
                throw new ServiceException("crm" + "PUBLIC-007");
            }
            Agreement example = new Agreement();
            example.setId(agrId);
            Agreement account = agreementService.queryById(example);
            result.put("rows", account);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


}
